package com.mycity.services;

import com.mycity.bean.User;
import com.mycity.dao.UserDao;

public class RegistrationService {
	private UserDao userDao=new UserDao();
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public User registerUser(String username, String name, String surname, String email, String password) throws Exception{
		User user;
		try {
			user = userDao.addUser(username, name, surname, email, password);

		} catch (Exception e) {
			return new User();
		}
		return user;
	}

	public boolean emailAvailable(String email) throws Exception{
		boolean available;
		boolean validEmail;
		User user = new User();
		try{
			available=!userDao.emailExists(email);
			validEmail=email.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");				
		}
		catch (Exception e) {
			throw new Exception("Error!");
		}
		return available&&validEmail;
	}
	
	public boolean usernameAvailable(String username)throws Exception{
		boolean available;
		User user = new User();
		try{
			available=!userDao.usernameExists(username);
		}
		catch (Exception e) {
			throw new Exception("Error!");
		}
		return available;
	}
}
